import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

import { getToken } from '@/utils/handleToken'

import { getLocalStorage } from '@/localstorage/storageHandle'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },

    {
      path: '/computes',
      name: 'computes',
      component:  () => import('../views/compute.vue')
    },

    {
      path: '/transitions',
      name: 'transitions',
      component:  () => import('../views/transitions.vue')
    },

    {
      path: '/keepalive',
      name: 'transitions',
      component:  () => import('../views/keepalive/keepAlives.vue')
    },

    {
      path: '/cycles',
      name: 'cycles',
      component:  () => import('../views/cycle/parent.vue')
    },

    {
      path: '/routers',
      name: 'routers',
      component: () => import('../views/Routers.vue')
    },

    {
      path: '/test',
      name: 'test',
      component: () => import('../views/TestView.vue')
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },

    {
      path: '/gaode',
      name: 'gaode',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/maps/gaode.vue')
    },
    {
      path: '/baidu',
      name: 'baidu',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/maps/baidu.vue')
    },
    {
      path: '/globals',
      name: 'globals',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/globals/index.vue')
    },
    {
      path: '/images',
      name: 'images',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/images/index.vue')
    }
  ]
})

router.beforeEach((to, from) => {

  const token = getLocalStorage('test');

  console.log(token)
  if(!token && to.name !=='about'){
    // about you add localstorage
    return {name:'about'}
  }

})

export default router
